define(['zepto', 'butterfly/view','../../bsl',
    'text!./zip.html'
], function($, View, bsl, Zip) {
    var me = null;
    var zip = "_doc/zip/index.zip";
    var unzip = "_doc/zip/index/";
    var view = View.extend({
        events: {
           "tap #openDoc":"OpenDoc",
           "tap #back":"iback",
           "tap #compressImage":"compressImage",
           "tap #compress":"compress",
           "tap #decompress":"decompress",
           "tap #clearZip":"clearZip"
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
        
        doCompress : function(){
            bsl.zip.compress( "_www/index.html", zip, function(){
                console.info( "操作成功！保存路径为："+zip );
            }, function(e){
                console.info( "操作失败："+e.message );
            } );
        },

        compress : function (){
            console.info( "压缩文件：" );
            bsl.io.resolveLocalFileSystemURL( zip, function(entry){
                console.info( "文件已存在，中止操作！" );
                bsl.nativeUI.confirm( "压缩文件已存在，是否立即删除", function(i){
                    if ( i.index == 0 ) {
                        entry.remove();
                    }
                } );
            }, function(e){
                doCompress();
            } );
        },

        doDecompress : function (){
            bsl.zip.decompress( zip, unzip, function(){
                console.info( "操作成功！保存路径为："+unzip );
            }, function(e){
                console.info( "操作失败："+e.message );
            } );
        },

        decompress : function (){
            console.info( "解压文件：" );
            bsl.io.resolveLocalFileSystemURL( unzip, function(entry){
                console.info( "目录已存在，中止操作！" );
                bsl.nativeUI.confirm( "解压目录已存在，是否立即删除", function(i){
                    if ( i.index == 0 ) {
                        entry.removeRecursively();
                    }
                } );
            }, function(e){
                bsl.io.resolveLocalFileSystemURL( zip, function(entry){
                    doDecompress();
                }, function(e){
                    bsl.nativeUI.alert( "压缩文件不存在，请先压缩文件！" );
                    console.info( "压缩文件不存在，中止操作！" );
                } );
            } );
        },

        clearZip : function(){
            console.info( "清空数据！" );
            bsl.io.resolveLocalFileSystemURL( "_doc/zip/", function(entry){
                entry.removeRecursively();
            } );
        },

        // 压缩图片
        compressImage : function (){
            console.info("开始压缩图片：");
            bsl.nativeUI.showWaiting();
            bsl.zip.compressImage({
                src:"_www/img/shake/1.jpg",
                dst:"_doc/cm.jpg",
                quality:20,
                overwrite:true,
                width:'50%',
                clip:{
                    top:"25%",
                    left:"25%",
                    width:"50%",
                    height:"50%"
                }
            },
            function(i){
                bsl.nativeUI.closeWaiting();
                display.style.display="block";
                clipimg.src=i.target;
                console.info("压缩图片成功："+JSON.stringify(i));
            },function(e){
                bsl.nativeUI.closeWaiting();
                console.info("压缩图片失败: "+JSON.stringify(e));
            });
        },

        iback: function(){
            window.history.go(-1);
        },

        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/zip', {
                trigger: true
            });
        },
        
    });
    return view;
});
